import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { localStorage } from "@/utils/local-storage";
NProgress.configure({ showSpinner: true })

// 导入路由组件
import index from '@/views/index.vue'
// import mock from '@/views/mock/index.vue'

import home from '@/views/home.vue'
import login from '@/views/login.vue'
import page404 from '@/views/404.vue'
import user from '@/views/system/user/index.vue'
import search from '@/views/search/index.vue'
import upcomingTasks from '@/views/upcoming-tasks/index.vue'
import arriveScanning from '@/views/warehouse/arrive-scanning/index.vue'
import enterTheWarehouse from '@/views/warehouse/enter-the-warehouse/index.vue'
import shiftoutTheWarehouse from '@/views/warehouse/shiftout-the-warehouse/index.vue'
import shiftoutByCirculationBox from '@/views/warehouse/shiftout-the-warehouse/shiftoutByCirculationBox.vue'
import putShelves from '@/views/warehouse/put-shelves/index.vue'
import graspingCodeComparison from '@/views/warehouse/grasping-code-comparison/index.vue'
import skuScan from '@/views/warehouse/sku-scan/index.vue'
import pick from '@/views/warehouse/pick/index.vue'
import pickOperate from '@/views/warehouse/pick/operate.vue'
import inboundScan from '@/views/warehouse/inbound/index.vue'
import standardInbound from '@/views/warehouse/inbound/standardInbound.vue'
import skuInbound from '@/views/warehouse/inbound/skuInbound.vue'
import fastInbound from '@/views/warehouse/inbound/fastInbound.vue'
import shelveScan from '@/views/warehouse/shelve/index.vue'
import skuShelve from '@/views/warehouse/shelve/skuShelve.vue'
import boxShelve from '@/views/warehouse/shelve/boxShelve.vue'
import refundScan from '@/views/warehouse/refund/index.vue'
import refundOperate from '@/views/warehouse/refund/operate.vue'
import ownerless from '@/views/warehouse/refund/ownerless.vue'
import outboundScan from '@/views/warehouse/outbound/index.vue'
import outboundOperate from '@/views/warehouse/outbound/operate.vue'
import refundReceive from '@/views/warehouse/refund-recevie/index.vue'
import cutOffProcessing from '@/views/warehouse/cutoff-processing/index.vue'
import page from '@/views/warehouse/cutoff-processing/page.vue'
import destruction from '@/views/warehouse/destruction/index.vue'
import operate from '@/views/warehouse/destruction/operate.vue'
import check from '@/views/warehouse/check/index.vue'
import checkOperate from '@/views/warehouse/check/operate.vue'
import { STORAGE_TOKEN_KEY } from '@/stores/mutation-type'
import shiftoutBySku from "@/views/warehouse/shiftout-the-warehouse/shiftoutBySku.vue";
import shiftoutByLocations from "@/views/warehouse/shiftout-the-warehouse/shiftoutByLocations.vue";

// 定义路由，每个路由都需要映射到一个组件
const routes: RouteRecordRaw[] = [
  {
    path: '/index',
    name: 'Index',
    component: index
  },
  // {
  //   path: '/mock',
  //   name: 'mock',
  //   component: mock
  // },
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: home
  },
  {
    path: '/login',
    name: 'Login',
    component: login
  }, {
    path: '/404',
    name: '404',
    component: page404
  }
  , { path: '/user', name: 'User', component: user }
  , { path: '/search', name: 'Search', component: search }
  , { path: '/upcomingTasks', name: 'UpcomingTasks', component: upcomingTasks }
  , { path: '/arriveScanning', name: 'ArriveScanning', component: arriveScanning }
  , { path: '/enterTheWarehouse', name: 'EnterTheWarehouse', component: enterTheWarehouse }
  , { path: '/shiftoutTheWarehouse', name: 'ShiftoutTheWarehouse', component: shiftoutTheWarehouse }
  , { path: '/shiftoutByLocation', name: 'shiftoutByLocation', component: shiftoutByLocations }
  , { path: '/shiftoutBySku', name: 'shiftoutBySku', component: shiftoutBySku }
  , { path: '/shiftoutByCirculationBox', name: 'shiftoutByCirculationBox', component: shiftoutByCirculationBox }
  , { path: '/putShelves', name: 'PutShelves', component: putShelves }
  , { path: '/graspingCodeComparison', name: 'GraspingCodeComparison', component: graspingCodeComparison }
  , { path: '/skuScan', name: 'SkuScan', component: skuScan }
  , { path: '/pick', name: 'Pick', component: pick }
  , { path: '/pickOperate', name: 'PickOperate', component: pickOperate }
  , { path: '/inboundScan', name: 'InboundScan', component: inboundScan }
  , { path: '/standardInbound', name: 'StandardInbound', component: standardInbound }
  , { path: '/skuInbound', name: 'SkuInbound', component: skuInbound }
  , { path: '/fastInbound', name: 'FastInbound', component: fastInbound }
  , { path: '/shelveScan', name: 'ShelveScan', component: shelveScan }
  , { path: '/skuShelve', name: 'SkuShelve', component: skuShelve }
  , { path: '/boxShelve', name: 'BoxShelve', component: boxShelve }
  , { path: '/refundScan', name: 'RefundScan', component: refundScan }
  , { path: '/refundOperate', name: 'RefundOperate', component: refundOperate }
  , { path: '/ownerless', name: 'Ownerless', component: ownerless }
  , { path: '/outboundScan', name: 'OutboundScan', component: outboundScan }
  , { path: '/outboundOperate', name: 'OutboundOperate', component: outboundOperate }
  , { path: '/refundReceive', name: 'refundReceive', component: refundReceive }
  , { path: '/cutOffProcessing', name: 'cutOffProcessing', component: cutOffProcessing }
  , { path: '/page', name: 'page', component: page }
  , { path: '/destruction', name: 'destruction', component: destruction }
  , { path: '/operate', name: 'operate', component: operate }
  , { path: '/check', name: 'check', component: check }
  , { path: '/checkOperate', name: 'checkOperate', component: checkOperate }
]

// 创建路由实例并传递 `routes` 配置
const router = createRouter({
  // history: createWebHistory(),
  history: createWebHashHistory(),
  routes,
  scrollBehavior() {
    // 始终滚动到顶部
    return { top: 0 }
  },
})

router.beforeEach((_to, _from, next) => {
  //判断是否登录
  if ((!_to.path.startsWith('/login')) && !localStorage.get(STORAGE_TOKEN_KEY)) {
    //未登陆+不在登陆页面
    console.log('未登陆+不在登陆页面')
    next()
    // next({ path: 'login?aa=123' ,params: { callback: _to.path } })
    return
  }
  if (_to.path == '/login' && localStorage.get(STORAGE_TOKEN_KEY)) {
    //已登陆+在登陆页面 跳转到home
    next({ path: '/' })
    return
  }
  NProgress.start(); // start progress bar
  if (_to.matched.length === 0) {  // 如果未匹配到路由
    _from.name ? next({ name: _from.name }) : next('/404')
    return
  }
  console.log('👙 '+_from.path+' 跳转 ->'+_to.path)
  next()  // 如果匹配到正确跳转
})

router.afterEach(() => {
  NProgress.done() // finish progress bar
})

// 导出路由实例，并在 `main.ts` 挂载
export default router
